package myJs.api

import japgolly.scalajs.react.extra.Ajax
import myJs.myPkg.MyFetch
import upickle.default._
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._
import shared.pojo.Pojo.ValidData
import myJs.Implicits._

/** Created by yz on 18/10/2022
  */
object Data {

  import shared.pojo.Pojo.data._

  val baseUrl = s"${Api.apiUrl}/user/data"

  def getAllExampleData(request: GetAllExampleData.Request) = {
    val url = s"${baseUrl}/getAllExampleData?softKind=${request.softKind}&fileName=${request.fileName}"
    Ajax
      .get(s"${url}")
      .setRequestContentTypeJson
      .send
      .asAsyncCallback
      .map { xhr =>
        read[List[ujson.Obj]](xhr.responseText)
      }
  }

  def getAllFileData(request: GetAllFileData.Request) = {
    val url = s"${baseUrl}/getAllFileData"
    Ajax
      .get(s"${url}?kind=${request.kind}")
      .setRequestContentTypeJson
      .send
      .asAsyncCallback
      .map { xhr =>
        read[List[ujson.Obj]](xhr.responseText)
      }
  }

  def getRowCols(request: GetRowCols.Request) = {
    val url = s"${baseUrl}/getRowCols?fileName=${request.fileName.encodeURI}"
    Ajax
      .get(s"${url}")
      .setRequestContentTypeJson
      .send
      .asAsyncCallback
      .map { xhr =>
        read[GetRowCols.Response](xhr.responseText)
      }
  }

  def getNumericColNames(request: GetNumericColNames.Request) = {
    val url = s"${baseUrl}/getNumericColNames?fileName=${request.fileName.encodeURI}"
    Ajax
      .get(s"${url}")
      .setRequestContentTypeJson
      .send
      .asAsyncCallback
      .map { xhr =>
        read[GetNumericColNames.Response](xhr.responseText)
      }
  }

}
